﻿using Microsoft.VisualBasic;
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VIA.Integration;
using VIA.SiMoJi.Domain.Electronics;

namespace VIA.SiMoJi.Presentation.ViewModel
{

    [AddINotifyPropertyChangedInterface]
    public class CylinderDebugViewModel
    {
        public CylinderEx Cylinder { get; }
        public string Name { get; set; }
        public string PushoutDO { get; set; }
        public string TakeBackDO { get; set; }
        public string PushoutSensorDI { get; set; }
        public string TakeBackSensorDI { get; set; }
        public bool IsPushout { get; set; }
        public bool IsTakeBack { get; set; }
        public bool IsPushoutSensorOn { get; set; }
        public bool IsBackSensorOn { get; set; }
        public CylinderDebugViewModel() { }
        public CylinderDebugViewModel(CylinderEx cylinder)
        {
            Cylinder = cylinder;
            Name = cylinder.Name;
            if (cylinder.PushOutSensor is CardIoPoint pt1)
            {
                PushoutSensorDI = $"DI.{pt1.PortNo}";
            }
            if (cylinder.BackSensor is CardIoPoint pt2)
            {
                TakeBackSensorDI = $"DI.{pt2.PortNo}";

            }
            IsPushout = cylinder.IsPushOut();
            IsTakeBack = cylinder.IsBack();
            IsPushoutSensorOn = IsPushout;
            IsBackSensorOn = IsTakeBack;
        }

        public async Task<bool> PushuOut()
        {
            bool b = await Cylinder.PushOutAsync();
            IsPushout = true;
            IsTakeBack = false;
            IsPushoutSensorOn = Cylinder.IsPushOut();
            IsBackSensorOn = Cylinder.IsBack();
            return b;
        }

        public async Task<bool> TakeBack()
        {
            bool b = await Cylinder.TakeBackAsync();
            IsPushout = false;
            IsTakeBack = true;
            IsPushoutSensorOn = Cylinder.IsPushOut();
            IsBackSensorOn = Cylinder.IsBack();
            return b;
        }

    }
}
